package com.zsck.pos.adapter;

import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chinaums.commoncheck.R;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.zsck.pos.bean.FeeEvent;
import com.zsck.pos.bean.FeeVOListBean;
import com.zsck.pos.bean.ThreadLiveEvent;

import org.jetbrains.annotations.NotNull;

public class ThirdFeeAdapter extends BaseNodeProvider {
    @Override
    public int getItemViewType() {
        return 3;
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_third;
    }

    @Override
    public void convert(@NotNull BaseViewHolder baseViewHolder, BaseNode baseNode) {
        FeeVOListBean entity = (FeeVOListBean) baseNode;
        baseViewHolder.setText(R.id.tv_fee, entity.getName())
                .setText(R.id.fee_amount, Double.toString(entity.getAmount()));
        CheckBox firstCheck = baseViewHolder.getView(R.id.third_check);
        firstCheck.setOnCheckedChangeListener(null);
        firstCheck.setChecked(entity.isChecked());
        firstCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                entity.setChecked(isChecked);
                LiveEventBus.get(ThreadLiveEvent.class)
                        .post(new ThreadLiveEvent(entity, 0));
                if (isChecked) {
                    LiveEventBus.get(FeeEvent.class)
                            .post(new FeeEvent(entity, true));
                } else {

                    LiveEventBus.get(FeeEvent.class)
                            .post(new FeeEvent(entity, false));
                }
                getAdapter().notifyDataSetChanged();
            }
        });
    }
}
